package struct.scene
{
	import collection.ActionCollection;
	import collection.DataCollection;
	
	import com.common.utils.Clock;
	import com.event.CommenEvent;
	
	import data.MapVO;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import struct.gameinfo.GameInfoModule;
	import struct.scene.items.MapItem;
	import struct.scene.ui.Scene;
	
	import utils.Alert;
	
	public class SceneModule extends EventDispatcher
	{
		public static var _instance:SceneModule;
		public var scene:Scene; //地图ui层
		public var mapVO:MapVO; //地图数据
		
		public function SceneModule()
		{
			super();
		}
		
		public static function getInstance():SceneModule
		{
			if (!_instance)
			{
				_instance = new SceneModule();
			}
			return SceneModule._instance;
		}
				
		public function setMission(missionID:int):void
		{
			// 绘制地图
			if (scene)
			{
				scene.dispose();
				Main.application.mapLayer.removeChild(scene);
			}
			//读取对应的地图数据
			mapVO = DataCollection.hmMap.get(String(1000 + missionID));
			
			scene = new Scene(Main.application.mapLayer.width, Main.application.mapLayer.height, mapVO);
			
			scene.initMapLayer(DataCollection.mission); //初始化地图
			scene.initGridlayer(mapVO.arrGridInfo); //初始化地格
			scene.initPlayerLayer(mapVO.arrPlayerPosition); //配置player和enemy
			
			Main.application.mapLayer.addChild(scene);
		}
		
		
		public function useMapItem(mapitem:MapItem):void
		{
			// TODO:  player吃到一个道具
			trace("捡到一个道具~");
			GameInfoModule.getInstance().addScore(mapitem.award);
		}
		
		public function missionComplete():void
		{
			trace("通关");
			DataCollection.gameStart = false; //游戏暂停
			if (DataCollection.mission < DataCollection.allMission)
			{
				//还有下一关
				this.addEventListener(ActionCollection.NEXTMISSION, setNextMission);
				Clock.getInstance().pushEvent(this, ActionCollection.NEXTMISSION, 3000);
			}else
			{
				Alert.showAlert("恭喜! 全部通关");
			}
		}
		
		protected function setNextMission(event:CommenEvent):void
		{
			DataCollection.gameStart = true; //游戏开始
			setMission(++DataCollection.mission);
		}
	}
}